/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.engine.func;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author tangli
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-6-8 下午2:11:38          tangli         1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class Func {
    private static Pattern propPattern = Pattern.compile("prop\\((\\d+,\\d+)\\)");
    private static Pattern attrPattern = Pattern.compile("attr\\((\\d+,\\d+)\\)");
    private static Pattern incPattern = Pattern.compile("inc\\((\\d+,\\d+),\\d+\\)");

    /**
     * 解析表达式中使用的资源指标，数据返回的格式 List<resId,propId> 例如:[60,30, 30,10]
     *
     * @param expr
     * @return
     */
    public synchronized static List<String> parsePropNodes(String expr) {
        List<String> nodes = new ArrayList<String>();
        nodes.addAll(parseNodes(propPattern, expr));
        nodes.addAll(parseNodes(incPattern, expr));
        return nodes;
    }

    /**
     * 解析表达式中使用的资源属性，数据返回的格式 List<resId,attrId> 例如:[60,30, 30,10]
     *
     * @param expr
     * @return
     */
    public synchronized static List<String> parseAttrNodes(String expr) {
        return parseNodes(attrPattern, expr);
    }

    /**
     *
     * @param pattern
     * @param expr
     * @return
     */
    public synchronized static List<String> parseNodes(Pattern pattern, String expr) {
        List<String> nodes = new ArrayList<String>();
        Matcher matcher = pattern.matcher(expr);
        while (matcher.find()) {
            nodes.add(matcher.group(1));
        }
        return nodes;
    }
}
